# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        root = None # 新链表的头结点
        while head:
            # 对每个结点，把ta的next置为当前新链表，新链表头结点置为当前结点，head移下一节点
            head.next, root, head = root, head, head.next
            # 如果不是python，需要引入缓存变量
            # tmp = head.next
            # head.next = root
            # root = head
            # head = tmp
        return root